package yy.com.yjboview.viewutil;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import yy.com.yjboview.R;

/**
 * @description: <描述当前版本功能>
 * <p>
 * 简单的继承view类--基础知识
 * 引用：http://www.idtkm.com/customview/customview1/
 * </p>
 * @author: yjbo
 * @date: 2016-08-03 11:09
 */
public class SimpleViewUtil2 extends View {

    //    static Canvas mcanvas;
    //创建画笔
    static Paint mPaint = new Paint();
    //宽高
    private static int mWidth;
    private static int mHeight;
    private static int status = -1;

    public SimpleViewUtil2(Context context, int i) {
        super(context);
        status = i;
    }

    public SimpleViewUtil2(Context context) {
        super(context);
        initPain();
    }

    public SimpleViewUtil2(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPain();
    }

    public SimpleViewUtil2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPain();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)//21版本
    public SimpleViewUtil2(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initPain();
    }

    private void initPain() {
        mPaint.setStyle(Paint.Style.FILL);//设置画笔类型
        mPaint.setAntiAlias(true);//抗锯齿
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    //布局1
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.d("yjbo-onLayout", "getLeft==" + getLeft());
    }

    //布局1
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    //绘制
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        mcanvas = canvas;
        canvas.translate(mWidth / 2, mHeight / 2);// 将画布坐标原点移动到中心位置
        //绘制坐标原点
        mPaint.setColor(Color.BLACK);//设置画笔颜色
        mPaint.setStrokeWidth(10);//为了看得清楚,设置了较大的画笔宽度
        canvas.drawPoint(0, 0, mPaint);

        switch (status) {
            case 0:
                //绘制坐标轴4个断点
                canvas.drawPoints(new float[]{
                        mWidth / 2 * 0.8f, 0
                        , 0, mHeight / 2 * 0.8f
                        , -mWidth / 2 * 0.8f, 0
                        , 0, -mHeight / 2 * 0.8f}, mPaint);
                break;
            case 1:
                //画x，y轴
                mPaint.setStrokeWidth(1);//恢复画笔默认宽度
                //绘制X轴
                canvas.drawLine(-mWidth / 2 * 0.8f, 0, mWidth / 2 * 0.8f, 0, mPaint);
                //绘制Y轴
                canvas.drawLine(0, mHeight / 2 * 0.8f, 0, -mHeight / 2 * 0.8f, mPaint);
                break;
            case 2:
                mPaint.setStrokeWidth(3);
                //绘制X轴箭头
                canvas.drawLines(new float[]{
                        mWidth / 2 * 0.8f, 0, mWidth / 2 * 0.8f * 0.95f, -mWidth / 2 * 0.8f * 0.05f,
                        mWidth / 2 * 0.8f, 0, mWidth / 2 * 0.8f * 0.95f, mWidth / 2 * 0.8f * 0.05f
                }, mPaint);
                //绘制Y轴箭头
                canvas.drawLines(new float[]{
                        -mWidth / 2 * 0.8f * 0.05f, -mHeight / 2 * 0.8f * 0.95f, 0, -mHeight / 2 * 0.8f,
                        mWidth / 2 * 0.8f * 0.05f, -mHeight / 2 * 0.8f * 0.95f, 0, -mHeight / 2 * 0.8f
                }, mPaint);

                break;
            case 3:
                paintXY(canvas);
                break;
            case 4:

                //缩放的效果----这些特效得先设置特效再设置显示什么形状，否则无效
                canvas.scale(1, -1);

                paintXY(canvas);

                break;
            case 5:
                paintRect(canvas);
                paintXY(canvas);
                break;
            case 6:
                //再绘制矩形
                paintRect(canvas);
                paintXY(canvas);
                //平移效果
                canvas.translate(200, 200);
                mPaint.setColor(Color.RED);
                //new RectF效果和直接使用drawRect效果一样
                canvas.drawRect(new RectF(-mWidth / 8, -mHeight / 8, mWidth / 8, mHeight / 8), mPaint);
                break;
            case 7:
                //再绘制矩形
                paintRect(canvas);
                paintXY(canvas);
                canvas.scale(0.5f, 0.5f);
                mPaint.setColor(Color.RED);
                //new RectF效果和直接使用drawRect效果一样
                canvas.drawRect(new RectF(-mWidth / 8, -mHeight / 8, mWidth / 8, mHeight / 8), mPaint);
                break;
            case 8:
                //再旋转绘制矩形
                paintRect(canvas);
                paintXY(canvas);
                canvas.scale(0.5f, 0.5f);
                canvas.rotate(90);
                mPaint.setColor(Color.RED);
                //new RectF效果和直接使用drawRect效果一样
                canvas.drawRect(new RectF(-mWidth / 8, -mHeight / 8, mWidth / 8, mHeight / 8), mPaint);
                break;
            case 9:
                //再旋转绘制矩形
                paintRect(canvas);
                paintXY(canvas);
                canvas.skew(1, 0.5f);
                mPaint.setColor(Color.RED);
                //new RectF效果和直接使用drawRect效果一样
                canvas.drawRect(new RectF(-mWidth / 8, -mHeight / 8, mWidth / 8, mHeight / 8), mPaint);
                break;
            case 10:
                /***
                 * 解释：
                 * canvas.save();
                 * canvas.rotate(90);
                 * canvas.drawCircle(200,100,r,mPaint);//圆心(200,0)
                 * canvas.restore();
                 *
                 * 这边是将rotate的操作保存下来，然后选择，其他的操作不受影响
                 */
                //保存画布，旋转90°，绘制一个圆，之后恢复画布，使用相同参数再绘制一个圆。可以看到在恢复画布前后，相同参数绘制的圆，
                // 分别显示在了坐标系的不同位置。
                paintXY(canvas);

                float point = Math.min(mWidth,mHeight)*0.06f/2;
                float r = point*(float) Math.sqrt(2);
                mPaint.setStyle(Paint.Style.STROKE);
                mPaint.setColor(Color.BLACK);
                canvas.save();
                canvas.rotate(90);
                canvas.drawCircle(200,100,r,mPaint);//圆心(200,0)
                canvas.restore();
                mPaint.setColor(Color.RED);
                canvas.drawCircle(200,0,r,mPaint);//圆心(200,0)
                break;
        }
        //如果不在Canvas里面添加invalidate();那么下次调用就不会更新，
        invalidate();
    }
    //绘制矩形
    private void paintRect(Canvas canvas) {
        mPaint.setStyle(Paint.Style.STROKE);//设置画笔类型
        mPaint.setStrokeWidth(3);
        canvas.drawRect(-mWidth / 8, -mHeight / 8, mWidth / 8, mHeight / 8, mPaint);
    }
    //x,y轴
    private void paintXY(Canvas canvas) {
        mPaint.setStrokeWidth(3);
        //绘制X轴箭头
        canvas.drawLines(new float[]{
                mWidth / 2 * 0.8f, 0, mWidth / 2 * 0.8f * 0.95f, -mWidth / 2 * 0.8f * 0.05f,
                mWidth / 2 * 0.8f, 0, mWidth / 2 * 0.8f * 0.95f, mWidth / 2 * 0.8f * 0.05f,
                -mWidth / 2 * 0.8f, 0, mWidth / 2 * 0.8f, 0
        }, mPaint);
        //绘制Y轴箭头
        canvas.drawLines(new float[]{
                -mWidth / 2 * 0.8f * 0.05f, -mHeight / 2 * 0.8f * 0.95f, 0, -mHeight / 2 * 0.8f,
                mWidth / 2 * 0.8f * 0.05f, -mHeight / 2 * 0.8f * 0.95f, 0, -mHeight / 2 * 0.8f,
                0, mHeight / 2 * 0.8f, 0, -mHeight / 2 * 0.8f
        }, mPaint);
    }

    //事件处理
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);

    }

//    //重绘
//    @Override
//    public void invalidate() {
//        super.invalidate();
//
//    }

    public static void changeDraw(int i) {
        status = i;
    }
}
